using System;
using System.Web.UI;
using NuSoft.Business;
using NuSoft.Business.Framework;

namespace NSFxSamples.Transactions
{
	public partial class Default : Page
	{
		private const int GOOD_ID = 35;
		private const int BAD_ID = -4;
		private Random random = new Random();


		protected void Page_Load(object sender, EventArgs e)
		{
			//
		}

		protected void btnFail_Click(object sender, EventArgs e)
		{
			DoTransWithEntityList(true);
		}
		protected void btnFail2_Click(object sender, EventArgs e)
		{
			DoTransWithSqlHelper(true);
		}
		protected void btnSucceed_Click(object sender, EventArgs e)
		{
			DoTransWithEntityList(false);
		}
		protected void btnSucceed2_Click(object sender, EventArgs e)
		{
			DoTransWithSqlHelper(false);
		}
		protected void btnFail3_Click(object sender, EventArgs e)
		{
			DoTransWithEntityListOfMixedClasses(true);
		}
		protected void btnSucceed3_Click(object sender, EventArgs e)
		{
			DoTransWithEntityListOfMixedClasses(false);
		}
		protected void btnFail4_Click(object sender, EventArgs e)
		{
			DoTransWithSqlHelperAndMixedClasses(true);
		}
		protected void btnSucceed4_Click(object sender, EventArgs e)
		{
			DoTransWithSqlHelperAndMixedClasses(false);
		}
		private string GenRandomString()
		{
			return random.Next(5000).ToString();
		}


		private void DoTransWithEntityListOfMixedClasses(bool fail)
		{
			try
			{
				EntityList<EntityBaseReadOnly> eList = new EntityList<EntityBaseReadOnly>();

				Address goodAddress = Address.CreateAddress();
				goodAddress.AddressLine1 = GenRandomString();
				goodAddress.City = GenRandomString();
				goodAddress.StateProvinceID = GOOD_ID;
				goodAddress.PostalCode = GenRandomString();
				goodAddress.rowguid = Guid.NewGuid();
				eList.Add(goodAddress);

				Department goodDept = Department.CreateDepartment();
				goodDept.Name = GenRandomString();
				goodDept.GroupName = GenRandomString();
				eList.Add(goodDept);

				Address badAddress = Address.CreateAddress();
				badAddress.AddressLine1 = GenRandomString();
				badAddress.City = GenRandomString();
				badAddress.StateProvinceID = fail ? BAD_ID : GOOD_ID; /* this could be an invalid StateProvinceID */
				badAddress.PostalCode = GenRandomString();
				badAddress.rowguid = Guid.NewGuid();
				eList.Add(badAddress);

				Address goodAddress2 = Address.CreateAddress();
				goodAddress2.AddressLine1 = GenRandomString();
				goodAddress2.City = GenRandomString();
				goodAddress2.StateProvinceID = GOOD_ID;
				goodAddress2.PostalCode = GenRandomString();
				goodAddress2.rowguid = Guid.NewGuid();
				eList.Add(goodAddress2);

				eList.Save();

				litFailMessage3.Text = "Success!";
			}
			catch (Exception e)
			{
				litFailMessage3.Text = "Failed transaction! <br />" + e.Message;
			}
		}
		private void DoTransWithSqlHelperAndMixedClasses(bool fail)
		{
			try
			{
				Address goodAddress = Address.CreateAddress();
				goodAddress.AddressLine1 = GenRandomString();
				goodAddress.City = GenRandomString();
				goodAddress.StateProvinceID = GOOD_ID;
				goodAddress.PostalCode = GenRandomString();
				goodAddress.rowguid = Guid.NewGuid();

				Department goodDept = Department.CreateDepartment();
				goodDept.Name = GenRandomString();
				goodDept.GroupName = GenRandomString();

				Address badAddress = Address.CreateAddress();
				badAddress.AddressLine1 = GenRandomString();
				badAddress.City = GenRandomString();
				badAddress.StateProvinceID = fail ? BAD_ID : GOOD_ID; /* this could be an invalid StateProvinceID */
				badAddress.PostalCode = GenRandomString();
				badAddress.rowguid = Guid.NewGuid();

				Address goodAddress2 = Address.CreateAddress();
				goodAddress2.AddressLine1 = GenRandomString();
				goodAddress2.City = GenRandomString();
				goodAddress2.StateProvinceID = GOOD_ID;
				goodAddress2.PostalCode = GenRandomString();
				goodAddress2.rowguid = Guid.NewGuid();


				using (SqlHelper helper = new SqlHelper())
				{
					helper.BeginTransaction();

					goodAddress.Save(helper);
					goodDept.Save(helper);
					badAddress.Save(helper);
					goodAddress2.Save(helper);

					helper.Commit();
				}


				litFailMessage4.Text = "Success!";
			}
			catch (Exception e)
			{
				litFailMessage4.Text = "Failed transaction! <br />" + e.Message;
			}
		}
		private void DoTransWithEntityList(bool fail)
		{
			try
			{
				EntityList<Address> addressList = new EntityList<Address>();

				Address goodAddress = Address.CreateAddress();
				goodAddress.AddressLine1 = GenRandomString();
				goodAddress.City = GenRandomString();
			goodAddress.StateProvinceID = GOOD_ID;
				goodAddress.PostalCode = GenRandomString();
				goodAddress.rowguid = Guid.NewGuid();
				addressList.Add(goodAddress);

				Address badAddress = Address.CreateAddress();
				badAddress.AddressLine1 = GenRandomString();
				badAddress.City = GenRandomString();
				badAddress.StateProvinceID = fail ? BAD_ID : GOOD_ID; /* this could be an invalid StateProvinceID */
				badAddress.PostalCode = GenRandomString();
				badAddress.rowguid = Guid.NewGuid();
				addressList.Add(badAddress);

				Address goodAddress2 = Address.CreateAddress();
				goodAddress2.AddressLine1 = GenRandomString();
				goodAddress2.City = GenRandomString();
				goodAddress2.StateProvinceID = GOOD_ID;
				goodAddress2.PostalCode = GenRandomString();
				goodAddress2.rowguid = Guid.NewGuid();
				addressList.Add(goodAddress2);

				addressList.Save();

				litFailMessage.Text = "Success!";
			}
			catch (Exception e)
			{
				litFailMessage.Text = "Failed transaction! <br />" + e.Message;
			}
		}
		private void DoTransWithSqlHelper(bool fail)
		{
			try
			{
				Address goodAddress = Address.CreateAddress();
				goodAddress.AddressLine1 = GenRandomString();
				goodAddress.City = GenRandomString();
				goodAddress.StateProvinceID = GOOD_ID;
				goodAddress.PostalCode = GenRandomString();
				goodAddress.rowguid = Guid.NewGuid();

				Address badAddress = Address.CreateAddress();
				badAddress.AddressLine1 = GenRandomString();
				badAddress.City = GenRandomString();
				badAddress.StateProvinceID = fail ? BAD_ID : GOOD_ID; /* this could be an invalid StateProvinceID */
				badAddress.PostalCode = GenRandomString();
				badAddress.rowguid = Guid.NewGuid();

				Address goodAddress2 = Address.CreateAddress();
				goodAddress2.AddressLine1 = GenRandomString();
				goodAddress2.City = GenRandomString();
				goodAddress2.StateProvinceID = GOOD_ID;
				goodAddress2.PostalCode = GenRandomString();
				goodAddress2.rowguid = Guid.NewGuid();

				using (SqlHelper helper = new SqlHelper())
				{
					helper.BeginTransaction();

					goodAddress.Save(helper);
					badAddress.Save(helper);
					goodAddress2.Save(helper);

					helper.Commit();
				}

				litFailMessage2.Text = "Success!";
			}
			catch (Exception e)
			{
				litFailMessage2.Text = "Failed transaction! <br />" + e.Message;
			}
		}
	}
}